package com.wang.web3;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.wang.web3.adapter.MarketPropsAdapter;
import com.wang.web3.bean.MarketPropsBean;
import com.wang.web3.data.service.Business;
import com.wang.web3.data.service.ICallBackListener;

import java.util.ArrayList;
import java.util.List;


/**
 * 道具列表
 */
public class MarketPropFragment extends Fragment {
    private RecyclerView mRv;
    private List<MarketPropsBean.DataBean> mPropListArrayList;
    private BaseQuickAdapter mPropAdapter;
    private MarketPropsBean propBean;

    public static MarketPropFragment newInstance() {
        MarketPropFragment fragment = new MarketPropFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_market_prop, container, false);
        mRv = view.findViewById(R.id.mRv);
        init();
        return view;
    }


    MarketPropsAdapter.PersonHolderListener personHolderListener = new MarketPropsAdapter.PersonHolderListener() {
        @Override
        public void onPersonItemClicked(View v, MotionEvent event, int position) {
            Intent i = new Intent(getContext(), PropsDetailActivity.class);
            i.putExtra("propId", propBean.getData().get(position).getId());
            i.putExtra("categoryName", propBean.getData().get(position).getCategoryName());
            i.putExtra("itempic", propBean.getData().get(position).getItemPic());
            startActivity(i);
        }
    };

    private void init() {
        mPropListArrayList = new ArrayList<>();
        mPropAdapter = new MarketPropsAdapter(mPropListArrayList, personHolderListener);
        //设置布局管理w
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        mRv.setLayoutManager(layoutManager);
//        mRv.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        mRv.setAdapter(mPropAdapter);
        doPropsTask("props", 1, 20);
    }

    public void doPropsTask(String currentTabs, int pageNum, int pageSize) {
        Business.getInstance().doPropsTask(currentTabs, pageNum, pageSize, new ICallBackListener() {
            @Override
            public void onSuccess(String mCallBackVo) {
                Gson mGson = new Gson();
                propBean = mGson.fromJson(mCallBackVo, MarketPropsBean.class);
                mPropListArrayList.addAll(propBean.getData());
                mPropAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFaild(String mCallBackVo) {

            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
//            getKittiesCount();
        }
    }

}
